package unq.tpi.persistencia.performanceEj.daos;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import unq.tpi.persistencia.performanceEj.model.Employee;
import unq.tpi.persistencia.util.SessionManager;

public class EmployeeDAO {

	public Employee getByName(final String name, final String lastName) {
		Session session = SessionManager.getSession();
		return (Employee) session
				.createQuery(
						"from Employee where firstName = :name and lastName = :lastName")
				.setParameter("name", name).setParameter("lastName", lastName)
				.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Employee> getTenEmployeesWithHigherSalaries() throws ParseException {
		Session session = SessionManager.getSession();
		Criteria criteria = session.createCriteria(Employee.class);
		Criterion restriccion = Restrictions.sqlRestriction("{alias}.to_date = '9999-01-01'");
		criteria.createCriteria("salaries" , "salaries").add(restriccion);
		
		criteria.addOrder(Order.asc("salaries.amount"));
		criteria.setMaxResults(10);
		return criteria.list();
	}

	public Employee getByCode(int id) {
		Session session = SessionManager.getSession();
		return (Employee) session.load(Employee.class, id);
	}

}
